<?php

/***
 * Class StatController
 * 数据统计
 */
class StatController extends ManageController{

    function init(){
        parent::init();
    }

    #任务费用
    function taskFeeAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );

        $condition = $today_data = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;

        $condition['start_time'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0, 0, 0, date("m"), 1, date("Y"));
        $condition['end_time'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) + 86399 : '';
        if (empty($condition['end_time']) || $condition['end_time'] < $condition['start_time'] + 86400) {
            $condition['end_time'] = mktime(0, 0, 0, date("m"), date("d") , date("Y"));
        }

        $taskfee_data_model = new TaskfeeDataModel();
        $pagesize = 100;
        $list = $taskfee_data_model->getListData($page, $pagesize, $condition);

        if($condition['end_time'] >= mktime(0, 0, 0, date("m"), date("d") , date("Y"))){
            #任务的完成情况
            $recome_app_model = new RecomAppModel();
            $today_data = $recome_app_model->getDaylyTotalFee();
        }



        #总计
        $total_sum = array(
            'date' => '总计',
            'limit_nums' => 0,      // 任务量
            'complete_nums' => 0,   // 完成数
            'task_fee' => 0,        // 执行费用
            'invite_fee' => 0,      // 师父分红
            'reg_fee' => 0,         // 新手红包
            'alive' => 0,         // 日活
            'money_water' => 0,     // 总流水
            'total_fee' => 0,       // 总费用
            'profit' => 0,          // 总利润
            'cpa' => 0,             // 平均CPA
            'draw_fee' => 0,       // 提现费用
            'draw_beg' => 0,       // 今日提现费用
            'new_task_nums' => 0,  //新增任务数
            'total_task_nums' => 0,  //总任务数
            'do_task_num' => 0,
            'udid_abnormal' => 0,
            'reg_do_num' => 0,
            'alive_week' => 0,
            'alive_half_month' => 0,
        );
        foreach ($list as $k => $row) {
            $total_sum['limit_nums'] += $row['limit_nums'];
            $total_sum['complete_nums'] += $row['complete_nums'];
            $total_sum['task_fee'] += $row['task_fee'];
            $total_sum['invite_fee'] += $row['invite_fee'];
            $total_sum['do_task_num'] += $row['do_task_num'];
            $total_sum['reg_fee'] += $row['reg_fee'];
            $total_sum['reg_do_num'] += $row['reg_do_num'];
            $total_sum['udid_abnormal'] += $row['udid_abnormal'];
            $total_sum['alive'] = 0;
            $total_sum['money_water'] += $row['money_water'];
            $total_sum['total_fee'] += $row['total_fee'];
            $total_sum['profit'] += $row['profit'];
            $total_sum['cpa'] += $row['cpa'];
            $total_sum['draw_fee'] += $row['draw_fee'];
            $total_sum['alive_week'] += $row['alive_week'];
            $total_sum['alive_half_month'] += $row['alive_half_month'];
            $total_sum['draw_beg'] += $row['draw_beg'];
            $total_sum['new_task_nums'] += $row['new_task_nums'];
            $total_sum['total_task_nums'] += $row['total_task_nums'];

        }

        $this->_view->list = $list;
        $this->_view->params = $condition;
        $this->_view->today_data = $today_data;
        $this->_view->total_sum = $total_sum;
        $this->_layout->meta_title = '任务费用汇总统计';
    }


    #单任务费用统计
    function taskfeePerdayAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $page_size = 20;

        $condition['task_id'] = !empty($_REQUEST['task_id']) ? intval($_REQUEST['task_id']) : 0;
        $condition['apple_id'] = !empty($_REQUEST['apple_id']) ? intval($_REQUEST['apple_id']) : 0;
        $condition['sale_name'] = !empty($_REQUEST['sale_name']) ? trim($_REQUEST['sale_name']) : '';
        $condition['channel'] = !empty($_REQUEST['channel']) ? trim($_REQUEST['channel']) : '';
        $condition['start_time'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
        $condition['start_date'] = date("Y-m-d", $condition['start_time']);
        $condition['plat'] = !empty($_REQUEST['plat']) ? trim($_REQUEST['plat']) : '';
        $condition['end_time'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';
        if (empty($condition['end_time']) || $condition['end_time'] < $condition['start_time'] + 86400) {
            $condition['end_time'] = $condition['start_time'] + 86400;
        }
        $condition['end_date'] = date("Y-m-d", $condition['end_time']);
        $task_ids = array();

        $search_param = array(
            'start_date' => $condition['start_time'],
            'end_date' => $condition['end_time'],
            'channel' => $condition['channel'],
            'sale_name' => $condition['sale_name'],
            'app_id' => $condition['task_id'],
            'appstore_id' => $condition['apple_id'],
            'porder' => 'start_date',
        );
        $recom_app_model = new RecomAppModel();
        $appinfoes = $recom_app_model->getListData($page, $page_size, $search_param);
        foreach ($appinfoes as $val) {
            $task_ids[] = $val['id'];
        }

        $recome_app_model = new RecomAppModel();
        $task_money_info = $recome_app_model->getPerdayFee($page, $page_size, $condition);  //关于app的相关性信息

        $condition['task_ids'] = array_keys($task_money_info);

        $user_exchange_model = new UserExchangeModel();
        $user_money_info = $user_exchange_model->getPerdayFee(1, 10000, $condition);  //关于用户领奖和师傅分红的钱

        $user_apply_hist_model = new UserApplyHistoryModel();
        foreach ($task_money_info as $k => $row) {
            // 获取已发奖数目
            $task_money_info[$k]['award_nums'] = $user_apply_hist_model->getAwardTotal($row['id']);
            $start_date = date('Ymd', $row['start_date']);
            $end_date = date('Ymd', $row['end_date']);
            $task_money_info[$k]['is_cross_day'] = $start_date == $end_date ? 0 : 1;  //是否跨天

            $task_money_info[$k]['task_type'] = $row['model'] == 1 ? '不对接': ( $row['model'] == 2 ? '激活上报':'点击回调'); // Beauty.W 增加任务类型

            $task_money_info[$k]['task_fee'] =  empty($user_money_info[$k]['task_fee']) ? 0 : $user_money_info[$k]['task_fee'];  //执行费用
            $task_money_info[$k]['invite_fee'] =  empty($user_money_info[$k]['invite_fee']) ? 0 : $user_money_info[$k]['invite_fee'];  //师傅分红
            $task_money_info[$k]['total_fee'] = $task_money_info[$k]['task_fee'] + $task_money_info[$k]['invite_fee'];  //总费用
            $task_money_info[$k]['cpa'] =  $row['complete_nums'] == 0 ? 0 : round($task_money_info[$k]['total_fee'] / $row['complete_nums'], 2);

            $task_money_info[$k]['aso_rank'] = array();//$key_words_info[$k];
            $task_money_info[$k]['money_water'] = $row['limit_nums']>$row['complete_nums'] ? $row['complete_nums']*$row['unit_price'] : $row['limit_nums']*$row['unit_price'];

        }

        #分页数据
        $totalnum = $recom_app_model->getListCount($search_param);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);


        $this->_view->list = $task_money_info;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '单任务费用统计';
    }

    function taskFeePerDayOutputAction()
    {
        $condition = array();
        $condition['task_id'] = !empty($_REQUEST['task_id']) ? intval($_REQUEST['task_id']) : 0;
        $condition['apple_id'] = !empty($_REQUEST['apple_id']) ? intval($_REQUEST['apple_id']) : 0;
        $condition['sale_name'] = !empty($_REQUEST['sale_name']) ? trim($_REQUEST['sale_name']) : '';
        $condition['channel'] = !empty($_REQUEST['channel']) ? trim($_REQUEST['channel']) : '';
        $condition['start_time'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
        $condition['start_date'] = date("Y-m-d", $condition['start_time']);
        $search['start_date'] = date("Ymd", $condition['start_time']);

        $condition['end_time'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';
        if (empty($condition['end_time']) || $condition['end_time'] < $condition['start_time'] + 86400) {
            $condition['end_time'] = $condition['start_time'] + 86400;
        }
        $condition['end_date'] = date("Y-m-d", $condition['end_time']);
        $search['end_date'] = date("Ymd", $condition['end_time']);
        $task_ids = array();
        if (!empty($condition['task_id']) || !empty($condition['apple_id']) || !empty($condition['sale_name'])) {
            $search_param = array('start_date' => $condition['start_time'], 'end_date' => $condition['end_time'],'channel' => $condition['channel']);
            if (!empty($condition['task_id'])) {
                $search_param['app_id'] = $condition['task_id'];
            }
            if (!empty($condition['apple_id'])) {
                $search_param['appstore_id'] = $condition['apple_id'];
            }
            if (!empty($condition['sale_name'])) {
                $search_param['sale_name'] = $condition['sale_name'];
            }
            $search_param['porder'] = 'start_date';
            $recom_app_model = new RecomAppModel();

            $appinfoes = $recom_app_model->getAdminListData(1, 1000, $search_param);

            foreach ($appinfoes as $val) {
                $task_ids[] = $val['id'];
            }

            $task_ids[] = '0';
        }


        $recome_app_model = new RecomAppModel();
        $task_money_info = $recome_app_model->getPerdayFee(0, 0, $condition);  //关于app的相关性信息

        $condition['task_ids'] = array_keys($task_money_info);
        $user_exchange_model = new UserExchangeModel();
        $user_money_info = $user_exchange_model->getPerdayFee(0, 0, $condition);  //关于用户领奖和师傅分红的钱

        #查询渠道对应的公司信息
        $channel = $recome_app_model->getChannelToCompany();

        #导出表头
        $title = array(
            "日期", "应用分类","任务ID", "对接类型", "来源名称","来源类型", "总任务量", "自家任务量", "自家完成数", "外放总量", "外放完成数", "执行费用",
            "师父分红", "总费用", "任务单价",
            "接入单价", "任务流水", "是否跨天",
            "平均CPA", "总利润",
            "产品名称", "appstoreId", "商务", "上线时间", "下线时间", "公司"
        );
        $showlist = array();
        foreach ($task_money_info as $k => $row) {
            $start_date = date('Ymd', $row['start_date']);
            $end_date = date('Ymd', $row['end_date']);
            $is_cross_day = $start_date == $end_date ? '否' : '是';  //是否跨天

            $task_type = $task_money_info[$k]['task_type'] = $row['model'] == 1 ? '不对接': ( $row['model'] == 2 ? '激活上报':'点击回调'); // Beauty.W 增加任务类型

            $task_fee =  empty($user_money_info[$k]['task_fee']) ? 0 : $user_money_info[$k]['task_fee'];  //执行费用
            $invite_fee =  empty($user_money_info[$k]['invite_fee']) ? 0 : $user_money_info[$k]['invite_fee'];  //师傅分红
            $total_fee = $task_fee + $invite_fee;  //总费用
            $cpa =  $row['complete_nums'] == 0 ? 0 : round($total_fee / $row['complete_nums'], 2);
            $money_water = $row['limit_nums']>$row['complete_nums'] ? $row['complete_nums']*$row['unit_price'] : $row['limit_nums']*$row['unit_price'];

            $channel_type = $row['channel_type'] == 1 ? '渠道' : ($row['channel_type'] == 2 ? '代理' : '直客' );

            // 写任务详情记录
            $showlist[] = array(
                $row['times'],$row['genres'], $row['id'] , $task_type, $row['channel'], $channel_type , $row['limit_nums'] + $row['ao_outside_num'], $row['limit_nums'], $row['complete_nums'], $row['ao_outside_num'], $row['outside_num'],$task_fee,
                $invite_fee, $total_fee, $row['price'],$row['unit_price'], $money_water,$is_cross_day,
                $cpa, $money_water-$total_fee, $row['product'], $row['appstore_id'],
                $row['sale_name'],  date("Y-m-d H:i", $row['start_date']), date("Y-m-d H:i", $row['end_date']),$channel[$row['channel']]['company']
            );
        }

        $file_name = '';
        if (!empty($condition['sale_name'])) {
            $file_name .= $condition['sale_name'] . '_';
        }
        if (!empty($condition['apple_id'])) {
            $file_name .= "产品" . $condition['apple_id'] . '_';
        }
        if (!empty($condition['task_id'])) {
            $file_name .= "ID" . $condition['task_id'] . '_';
        }
        $file_name .= " 单任务费用_" . $condition['start_date'] . '-' . $condition['end_date'];
        $this->excelOutput($file_name, $title, $showlist);
    }


    #优化效果导出
    function taskAsoResultAction()
    {

        $condition = array();
        $condition['task_id'] = !empty($_REQUEST['task_id']) ? intval($_REQUEST['task_id']) : 0;
        $condition['apple_id'] = !empty($_REQUEST['apple_id']) ? intval($_REQUEST['apple_id']) : 0;
        $condition['channel'] = !empty($_REQUEST['channel']) ? trim($_REQUEST['channel']) : 0;
        $condition['sale_name'] = !empty($_REQUEST['sale_name']) ? trim($_REQUEST['sale_name']) : '';
        $condition['start_time'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
        $condition['start_date'] = date("Y-m-d", $condition['start_time']);
        $search['start_date'] = date("Ymd", $condition['start_time']);

        $condition['end_time'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : '';
        if (empty($condition['end_time']) || $condition['end_time'] < $condition['start_time'] + 86400) {
            $condition['end_time'] = $condition['start_time'] + 86400;
        }
        $condition['end_date'] = date("Y-m-d", $condition['end_time']);
        $search['end_date'] = date("Ymd", $condition['end_time']);

        $recom_app_model = new RecomAppModel();

        $task_ids = array();
        $search_param = array('start_date' => $condition['start_time'], 'end_date' => $condition['end_time'],'channel' => $condition['channel']);
        if (!empty($condition['task_id'])) {
            $search_param['app_id'] = $condition['task_id'];
        }
        if (!empty($condition['apple_id'])) {
            $search_param['appstore_id'] = $condition['apple_id'];
        }
        if (!empty($condition['sale_name'])) {
            $search_param['sale_name'] = $condition['sale_name'];
        }
        $search_param['porder'] = 'start_date';
        $appinfoes = $recom_app_model->getAdminListData(1, 10000, $search_param);

        foreach ($appinfoes as $val) {
            $task_ids[] = $val['id'];
        }

        $task_ids[] = '0';

        $task_types = $recom_app_model->getTaskType($task_ids);

        // 处理每个任务的最后一个时间
        $timestamp_list = array();
        $user_apply_history_model = new UserApplyHistoryModel();
        $tmp_ret = $user_apply_history_model->getMaxcompleteTimeByTaskids($task_ids);
        foreach ($tmp_ret as $stmp_record){
            $timestamp_list[ $stmp_record['app_id'] ] = $stmp_record;
        }

        // 统计任务关键词的实际跑量
        $keyword_real_nums = array();
        $tmp_ret = $user_apply_history_model->getKeywordNums($task_ids);
        foreach ($tmp_ret as $tmp_record){
            $keyword_real_nums[ $tmp_record['app_id'] ][ $tmp_record['keyword'] ] = $tmp_record['num'];
        }

        // 根据任务ID获取到ASO优化效果数据
        $task_keyword_model = new TaskKeywordModel();
        $keywords_list = $task_keyword_model->getDataByTaskids($task_ids);  //获取多关键词

        #师傅分红
        $user_exchange_model = new UserExchangeModel();
        $user_money_info = $user_exchange_model->getPerdayFee(1, 10000, $condition);  //关于用户领奖和师傅分红的钱

        // 根据上面的信息，拼装导出excel的数据
        $excelData = array();
        $title = array(
            '任务ID', "产品分类", "产品名称",  '关键词', '热度', "对接平台", "对接类型", '下单量级',  '完成量级', '上线日期', '上线时间',
            '上线前11排名',    '上线后11排名',  '关键词11变动',  '11变动抓取时间',  '上线前12排名',    '上线后12排名',  '关键词12变动',  '12变动抓取时间','接入单价', '对接人', 'appstore_id', //
            '总流水', '总成本', '总利润');

        foreach ($appinfoes as $appinfo){
            $invite_fee = empty($user_money_info[ $appinfo['id'] ]['invite_fee']) ? '0' : $user_money_info[ $appinfo['id'] ]['invite_fee'];
            foreach ($keywords_list[ $appinfo['id'] ] as $idx => $task_keyword){
                $excelData[] = array(
                    $appinfo['id'], // 任务ID
                    $appinfo['genres'],$appinfo['product'],  $task_keyword['keyword'],  $task_keyword['hot'],
                    $task_types[$appinfo['id']]['channel'], $task_types[ $appinfo['id'] ]['type'],
                    $task_keyword['num'], $keyword_real_nums[ $appinfo['id'] ][ $task_keyword['keyword'] ],
                    date('Y/m/d', $appinfo['start_date']),   date('H:i', $appinfo['start_date']),    // 关键词，产品名称，上线日期，上线时间


                    $task_keyword['rank'],$task_keyword['aso_rank'],  //11之前的排名和之后的排名
                    $task_keyword['rank']-$task_keyword['aso_rank'],  //11排名变动
                    date('Y-m-d H:i', $task_keyword['up_rank_time']),    // 11总榜抓取时间

                    $task_keyword['rank_12'],$task_keyword['aso_rank_12'],  //12之前的排名和之后的排名
                    $task_keyword['rank_12']-$task_keyword['aso_rank_12'],  //12排名变动
                    empty($task_keyword['rank_time_12']) ? '0' : date('Y-m-d H:i', $task_keyword['rank_time_12']),    // 12总榜抓取时间

                    $appinfo['unit_price'],  $appinfo['sale_name'], $appinfo['appstore_id'],


                    $keyword_real_nums[ $appinfo['id'] ][ $task_keyword['keyword'] ]*$appinfo['unit_price'], //总流水
                    $idx == 0 ? ($invite_fee+$appinfo['price']*$keyword_real_nums[ $appinfo['id'] ][ $task_keyword['keyword'] ]) : 0,   //总成本
                    $idx == 0 ? ($keyword_real_nums[ $appinfo['id'] ][ $task_keyword['keyword'] ]*$appinfo['unit_price']-$appinfo['unit_price']*$keyword_real_nums[ $appinfo['id'] ][ $task_keyword['keyword'] ]) : ($keyword_real_nums[ $appinfo['id'] ][ $task_keyword['keyword'] ]*$appinfo['unit_price']-$appinfo['price']*$keyword_real_nums[ $appinfo['id'] ][ $task_keyword['keyword'] ])   //    利润
                );
            }
        }

        $file_name = '';
        if (!empty($condition['sale_name'])) {
            $file_name .= $condition['sale_name'] . '_';
        }
        if (!empty($condition['apple_id'])) {
            $file_name .= "产品" . $condition['apple_id'] . '_';
        }
        if (!empty($condition['task_id'])) {
            $file_name .= "ID" . $condition['task_id'] . '_';
        }
        $file_name .= " ASO优化效果_" . $condition['start_date'] . '-' . $condition['end_date'];
        $this->excelOutput($file_name, $title, $excelData);
    }


    #留存任务统计
    function remaintaskAction()
    {
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );

        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $page_size = 10;
        $condition = array();
        $condition['recom_id'] = !empty($_REQUEST['task_id']) ? intval($_REQUEST['task_id']) : 0;
        $condition['start_time'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0, 0, 0, date("m"), 1, date("Y"));
        $condition['end_time'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : $condition['start_time'] + 86400;

        #留存任务表里存的时间是ymd
        $condition['start_date'] = date("Ymd", $condition['start_time']);
        $condition['end_date'] = date("Ymd", $condition['end_time']);

        $remain_task_model = new RemainTaskModel();
        $list = $remain_task_model->getTaskList( $condition);

        #分页处理
        $totalnum = $remain_task_model->getTaskCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->list = $list;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '留存任务统计';
    }


    #大使数据列表
    function elcheeAction(){
        $condition = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['sort'] = !empty($_REQUEST['sort']) ? trim($_REQUEST['sort']) : '';
        $condition['date'] = !empty($_REQUEST['date']) ? intval($_REQUEST['date']) : date('Ymd',time()-86400);

        $invite_top500_model = new InviteTop500Model();
        $page_size = 20;
        $data = $invite_top500_model->getListByAdmin($page, $page_size, $condition);

        #分页处理
        $totalnum = $invite_top500_model->getListByAdminCount($condition);

        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->date = $invite_top500_model->getDate();
        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = 'Top500大使列表';
    }

    #大使数据导出
    function elcheeOutputAction(){
        $condition = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $condition['uid'] = !empty($_REQUEST['uid']) ? intval($_REQUEST['uid']) : '0';
        $condition['sort'] = !empty($_REQUEST['sort']) ? trim($_REQUEST['sort']) : '';
        $condition['date'] = !empty($_REQUEST['date']) ? intval($_REQUEST['date']) : date('Ymd',time()-86400);

        $invite_top500_model = new InviteTop500Model();
        $data = $invite_top500_model->getListByAdmin($page, 500, $condition);

        $title = array("UID","手机号码","累计收入","邀请人数","注册时间","IP","收徒变动情况");
        $showlist = array();
        foreach ($data as $row) {
            $showlist[] = array($row['uid'] ,$row['mobile'] ,$row['invite_add_up'] ,$row['invite_num'] ,date("Y.m.d H:i:s",$row['regtime']) ,$row['remote_ip'] ,$row['diff']);
        }
        $file_name = "Top500大使统计_" . $condition['date'];
        $this->excelOutput($file_name, $title, $showlist);
    }

    #链接统计列表
    function linkAction(){
        $condition = array();
        $title = isset($_REQUEST['title']) ? trim($_REQUEST['title']) : '';
        $page = isset($_REQUEST['page']) ? intval($_REQUEST['page']) : 1;
        $id = !empty($_REQUEST['id']) ? intval($_REQUEST['id']) : '0';

        if (!empty($title)) {
            $condition['title'] = $title;
        }
        if ($id > 0) {
            $condition['id'] = $id;
        }

        $page_size = 20;
        $click_obj = new ClickStatModel();
        $data = $click_obj->getListByAdmin($page, $page_size, $condition);

        #分页处理
        $totalnum = $click_obj->getListByAdminCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->rowsets = $data;
        $this->_view->params = $condition;
        $this->_layout->meta_title = '链接统计';
    }


    function addLinkAction()
    {
        $id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
        $title = isset($_REQUEST['title']) ? trim($_REQUEST['title']) : '';
        $third_url = isset($_REQUEST['third_url']) ? trim($_REQUEST['third_url']) : '';

        $click_obj = new ClickStatModel();
        $stat_info = array();
        if ($id != 0) {
            $stat_info = $click_obj->getDataById($id);
        }
        if ($this->getRequest()->isPost()) {
            $data = array(
                'title' => $title,
                'created_at' => time(),
                'third_url' => $third_url,
            );
            if (!empty($stat_info['id'])) {
                $click_obj->updateData($data, $stat_info['id']);
            } else {
                $click_obj->addData($data);
            }
            $this->set_flush_message("添加/修改统计链接成功");
            $this->redirect('/manage/stat/link/');
            return FALSE;

        }
        $this->_view->stat_info = $stat_info;
        $this->_layout->meta_title = '编辑/添加统计链接列表';
    }

    function outStatAction(){
        $this->_layout->javascript_block = array(
            '/js/jquery.datetimepicker.js'
        );

        $this->_layout->css_block = array(
            '/css/jquery.datetimepicker.css'
        );
        $condition = array();
        $page = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $page_size = 20;

        $condition['task_id'] = !empty($_REQUEST['task_id']) ? intval($_REQUEST['task_id']) : 0;
        $condition['app_id'] = !empty($_REQUEST['apple_id']) ? intval($_REQUEST['apple_id']) : 0;
        $condition['sale_name'] = !empty($_REQUEST['sale_name']) ? trim($_REQUEST['sale_name']) : '';
        $condition['channel'] = !empty($_REQUEST['channel']) ? trim($_REQUEST['channel']) : '';
        $condition['start_time'] = !empty($_REQUEST['start_time']) ? $_REQUEST['start_time'] : 0;
        $condition['end_time'] = !empty($_REQUEST['end_time']) ? $_REQUEST['end_time'] : 0;
        if(empty($condition['start_time'])){
            $condition['start_time'] = !empty($_REQUEST['start_date']) ? strtotime($_REQUEST['start_date']) : mktime(0, 0, 0, date("m"), date("d") - 1, date("Y"));
        }
        if(empty($condition['end_time'])){
            $condition['end_time'] = !empty($_REQUEST['end_date']) ? strtotime($_REQUEST['end_date']) : mktime(0, 0, 0, date("m"), date("d"), date("Y"));
        }

        $out_task_model = new OutTaskModel();
        $list = $out_task_model->getOutStatList($page, $page_size, $condition);



        #分页数据
        $totalnum = $out_task_model->getOutStatCount($condition);
        $pagination = $this->getPagination($totalnum, $page, $page_size);
        $this->_view->page = $page;
        $this->_view->pager = new System_Page($this->now_url, $condition, $pagination);

        $this->_view->params = $condition;
        $this->_view->list = $list;
        $this->_layout->meta_title = '外放任务费用统计';
    }
}
?>